// 失去焦点金额格式化
export function amountFormat(value) {
    if (!value || value == 0)
        return 0;
    let temp = Number(value) || null;
    // this.totalprice = this.priceFormat(temp);
    return priceFormat(temp);
}
// 获得焦点金额去掉格式
export function clearFormat(value) {
    if (!!value) {
        // this.totalprice = this.delcommafy(el.target.value);
        return delcommafy(value);
    } else {
        totalprice = null;
    }
}
export function priceFormat(num, n) {
    n = n || 2;
    let symbol = ",";
    if (num === null) return num;
    if (typeof num !== "number")
        throw new TypeError("num参数应该是一个number类型");
    if (n < 0) throw new Error("参数n不应该小于0");
    let hasDot = parseInt(num) != num; //这里检测num是否为小数，true表示小数
    let m = n != undefined && n != null ? n : 1;
    num =
        m == 0
            ? num.toFixed(m) + "."
            : hasDot
                ? n
                    ? num.toFixed(n)
                    : num
                : num.toFixed(m);
    symbol = symbol || ",";
    num = num
        .toString()
        .replace(/(\d)(?=(\d{3})+\.)/g, function (match, p1, p2) {
            return p1 + symbol;
        });
    if (n == 0 || (!hasDot && !n)) {
        //如果n为0或者传入的num是整数并且没有指定整数的保留位数，则去掉前面操作中的小数位
        num = num.substring(0, num.indexOf("."));
    }
    return num;
}

//去除千分位中的‘，’
export function delcommafy(num) {
    if (!num) return num;
    num = num.toString();
    num = num.replace(/,/gi, "");
    if (num.indexOf(".00") > 0) num = parseInt(num);
    return num;
}

export function dealBigMoney(n) {
    if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法";
    let unit = "千百拾亿千百拾万千百拾元角分",
        str = "";
    n += "00";
    let p = n.indexOf(".");
    if (p >= 0) {
        n = n.substring(0, p) + n.substr(p + 1, 2);
        unit = unit.substr(unit.length - n.length);
    }

    for (var i = 0; i < n.length; i++)
        str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i);
    return str
        .replace(/零(千|百|拾|角)/g, "零")
        .replace(/(零)+/g, "零")
        .replace(/零(万|亿|元)/g, "$1")
        .replace(/(亿)万|壹(拾)/g, "$1$2")
        .replace(/^元零?|零分/g, "")
        .replace(/元$/g, "元整");
}